package hu.myai.triggers;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.DummyIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Trigger is the second procession stage after indicator for evaluate a stock.
 * Aim of a trigger is; depend on indicators output check if the pre-implemented
 * correlations and levels should indicate a sell or buy. Each trigger class
 * generates a "suggested trade" event.
 * 
 * @author bozbalint
 * 
 * */
public class DummyTrigger extends AbstractTrigger {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * Reference id for use the register indicator value in the calculation
	 * method
	 * */
	private String dummyID;

	/**
	 * you have to create a setter for all indicator parameter. In the setter
	 * you have to register the the indicator with its parameter.<br>
	 * */
	public void setDummyIndicatorParameter(double parameter) {

		/**
		 * register() is a method for initialize indicators. Framework will
		 * configure the indicator that provides the inputs to the trigger. eg:
		 * xxxXXXxxID = register(new XXXXXIndicator(parameter));
		 * */
		dummyID = register(new DummyIndicator(parameter));
	}


	public DummyTrigger(String name, AbstractTickProvider tick) {
		super(name, tick);
	}

	/**
	 * Main method for implement trigger functionality
	 * */
	@Override
	protected TriggerVO triggerCalculation() {
		/**
		 * - indicatorMap: contains all received indicator value<br>
		 * - indicatorNameList: contains all registered indicator name. You can
		 * use at indicatorMap
		 * */

		double indValue = 0;

		IndicatorVO indicatorVO = indicatorMap.get(dummyID);

		// get the same indicator value as the previous line
		for (String indicatorName : indicatorNameList) {
			indicatorVO = indicatorMap.get(indicatorName);
			indValue += indicatorVO.getValue();
		}
		indValue = indValue / indicatorNameList.size();

		TriggerVO triggerVO = null;

		// average
		if (indValue > lastTick.getPivot()) {
			triggerVO = new TriggerVO();
			triggerVO.setTick(lastTick);
			triggerVO.setTradeType(0);
			triggerVO.setRisk(5);
			log.info("TriggerVO: " + triggerVO);
		}


		return triggerVO;
	}
}
